package advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.Arrays;

public class AroundService implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("========================环绕增强启动=====================");
        Object object = invocation.getThis();//获取目标方法所在的类
        Method method = invocation.getMethod();//被调用的方法
        Object[] args = invocation.getArguments();//方法的入参
        AccessibleObject staticPart = invocation.getStaticPart();//静态部分
        System.out.println(object.toString()+"的"+method.getName()+"方法的参数是："+
                Arrays.toString(args)+"静态部分："+staticPart.toString());
        try {
            Object returnVal = invocation.proceed();
            System.out.println("========================环绕增强结束=====================");
            return returnVal;
        } catch (Exception e) {
            System.out.println("==========环绕增强捕获异常====================");
            throw e;
        }
    }
}
